﻿#ifndef SerialPinch_H
#define SerialPinch_H

#include <QDebug>
#include <QObject>
#include <QSerialPort>
#include <QVariantMap>
#include <string>
#include "RobotCmdNode.hpp"

#define BUFF_LEN (1024)

class Commander;
class CommanderOld;

class SerialPinch : public QObject {
    Q_OBJECT
public:
    explicit SerialPinch(int argc, char** argv, QObject* parent = nullptr);
    ~SerialPinch();

public slots:
    void write(unsigned char* data, int len);

    void setCom(const std::string& comName, int maxI, int cmderType);

    void openCom();

    void closeCom();

    void spin45();

    void songkai();

    void suojin180x9();

    void callSpinBack45();

    void callStop();

    void callResetPos();

private slots:
    void spin45_p();
    void songkai_p();
    void suojin180x9_p();
    void spinBack45();
    void stop();
    void resetPos();

    // for cmderOld begin
    void spin45_p_old();
    void songkai_p_old();
    void suojin180x9_p_old();
    void spinBack45_old();
    // for cmderOld end

    void onReadReady();

    void onJudge(const QByteArray& ba);

private:
    void rosSpinThread();

private:
    int argc_;
    char** argv_;
    std::shared_ptr<RobotCmdNode> m_robotCmdNode;
    std::thread m_ros_spin_thread;

    QSerialPort* m_com;
    int m_nMaxI;
    int m_nCmderType;  // 1: old,  2: new

    Commander* m_cmder;

    CommanderOld* m_cmderOld;

    quint8 m_buff[BUFF_LEN];
    quint32 m_nBuffIndex;
};

#endif  // SerialPinch_H
